﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace easteradventure
{
  /// <summary>
  /// This is a game component that implements IUpdateable.
  /// </summary>
  public class Menu : DrawableGameComponent
  {
    Game1 game;

    const int MENU_OPTION_COUNT = 10;

    SpriteFont menuFont;
    Texture2D selArrow;
    MenuOption[] menu;
    byte menuOptionCount;
    SpriteBatch spriteBatch;
    int selection;
    Vector2 selPos,selPosOffset;
    Boolean selChanged;

    KeyboardState oldkbState;

    public Menu(Game1 game)
      : base(game)
    {
      this.game = game;
      menu = new MenuOption[MENU_OPTION_COUNT];
      spriteBatch = new SpriteBatch(game.GraphicsDevice);
      selection = 0;
      selChanged = true;
    }

    /// <summary>
    /// Allows the game component to perform any initialization it needs to before starting
    /// to run.  This is where it can query for any required services and load content.
    /// </summary>
    public override void Initialize()
    {
      // TODO: Add your initialization code here
      menu[menuOptionCount++] = new MenuOption("Start",new Vector2(320,240));
      menu[menuOptionCount++] = new MenuOption("Quit", new Vector2(320,360));
      menu[menuOptionCount++] = new MenuOption("fish", new Vector2(150,400));

      selPosOffset = new Vector2(-25.0f,0.0f);

      oldkbState = Keyboard.GetState();

      base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
      menuFont = game.Content.Load<SpriteFont>("MenuFont");
      //titlePic = Game.Content.Load<Texture2D>("");
      selArrow = Game.Content.Load<Texture2D>("Arrow");
      // TODO: use this.Content to load your game content here
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
      // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game component to update itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    public override void Update(GameTime gameTime)
    {
      // TODO: Add your update code here
      KeyboardState kbstate = Keyboard.GetState();
      if (kbstate.IsKeyDown(Keys.Up) && !oldkbState.IsKeyDown(Keys.Up))
      {
        selection--;
        selChanged = true;
      }
      if (kbstate.IsKeyDown(Keys.Down) && !oldkbState.IsKeyDown(Keys.Down))
      {
        selection++;
        selChanged = true;
      }
      if (kbstate.IsKeyDown(Keys.Enter) && !oldkbState.IsKeyDown(Keys.Enter))
      {
        game.switchScreen();
      }
      if (selChanged)
      {
        selChanged = false;
        selection = (int)MathHelper.Clamp(selection, 0, menuOptionCount - 1);
        selPos = new Vector2(menu[selection].Pos.X + selPosOffset.X, menu[selection].Pos.Y + selPosOffset.Y);
      }

      oldkbState = kbstate;
      base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    public override void Draw(GameTime gameTime)
    {
      spriteBatch.Begin(); 
      for (int i = 0; i < menuOptionCount; i++)
      {
        if (selection == i) spriteBatch.Draw(selArrow, selPos,Color.Orange);
        spriteBatch.DrawString(menuFont,menu[i].Text,menu[i].Pos,Color.Beige);
      }
      spriteBatch.End();

      base.Draw(gameTime);
    }


    private class MenuOption
    {
      String text;
      Vector2 pos;

      public MenuOption(String text, Vector2 pos)
      {
        this.text = text;
        this.pos = pos;
      }

      public String Text
      {
        get
        {
          return text;
        }
      }
      public Vector2 Pos
      {
        get
        {
          return pos;
        }
      }
    }

    }

  
}